package com.socket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Yancy
 * @program springioctest
 * @create 2021-01-17
 */
public class LoginServer {
  /** 服务器端 */
  public static void main(String[] args) throws IOException {

    // 指定监听端口
    ServerSocket serverSocket = new ServerSocket(8888);
    int i = 1;
    while (true) {
      // 监听请求,如果没有用户的请求，就在此等待，代码阻塞
      Socket socket = serverSocket.accept();

      Thread thread = new LoginThread(socket);
      thread.start();
      InetAddress ia = socket.getInetAddress();
      System.out.println("第" + (i++) + "个用户，ip地址是：" + ia.getHostAddress());
      //        // 接收来自客户端的数据并输出
      //        InputStream is = socket.getInputStream();
      //        DataInputStream dis = new DataInputStream(is);
      //        String result = dis.readUTF();
      //        System.out.println(result);
      //
      //        // 发送反馈数据给客户端
      //        OutputStream os = socket.getOutputStream();
      //        DataOutputStream dos = new DataOutputStream(os);
      //        dos.writeUTF("登录成功");
      //
      //        dos.close();
      //        os.close();
      //        dis.close();
      //        is.close();
      //        socket.close();
      //        serverSocket.close();
    }
  }
}
